/**
 * Created by shc@lierda.com on 2017/1/11.
 */
var DB =  require('../db/db');

/**获取商品分类*/
exports.getProductCate=function(req,res,msg){
    var sql="select * from k_category where parent_id = '0'";
    console.log(sql);
    DB.query(sql,function(er,list){
        console.log("----"+JSON.stringify(list));
        if(!er){
            msg.success=true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg=er;
            res.json(msg);
        }
    });
};

/**根据分类的id获取分类下的列表信息*/
exports.getCateproinfo = function (req,res,msg) {
    var parent_id = req.body.parent_id;
    var limit = req.body.limit;
    if(parent_id){
        var sql="SELECT id,picUrl,countryid FROM k_category WHERE parent_id='"+ parent_id +"' order by sort_order LIMIT "+limit+",2";
        console.log(sql);
        DB.query(sql,function(er,list){
            console.log("----"+JSON.stringify(list));
            if(!er){
                msg.success=true;
                msg.data = list;
                res.json(msg);
            }else{
                msg.msg=er;
                res.json(msg);
            }
        });
    } else{
        msg.msg="参数有误";
        res.json(msg);
    }
};

/**根据cate_id获取商品详情*/
exports.getGoodsInfoByCateid = function(req,res,msg){
    var cate_id = req.body.cate_id;
    var limit = req.body.limit;
    var countryid = req.body.countryid;
    if (cate_id){
        if(countryid != "null"){
            var sql = "SELECT *,shop_price as ProPrice FROM k_goods  WHERE country='" + countryid +"' AND is_on_sale = 1 order by goods_country_sort LIMIT " + limit + ",6" ;
        }else{
            var sql = "SELECT *,shop_price as ProPrice FROM k_goods  WHERE cat_id='" + cate_id +"' AND is_on_sale = 1 order by sort_order LIMIT " + limit + ",6";
        }
        console.log(sql);
        DB.query(sql,function(er,list){
            console.log("----"+JSON.stringify(list));
            if(!er){
                msg.success=true;
                msg.data = list;
                res.json(msg);
            }else{
                msg.msg=er;
                res.json(msg);
            }
        });
    } else {
        msg.msg="参数有误";
        res.json(msg);
    }
};


/**根据商品的id获取商品详情*/
exports.getGoodsDetailByid = function(req,res,msg){
    var goodsid = req.body.id;
    if (goodsid){
        var sql="select g.*,gs.price,gs.person_number,gs.action_start_time,gs.action_end_time,gc.gc_name,b.brand_name from k_goods as g "
            +" LEFT JOIN k_goods_shop as gs ON g.id = gs.goods_id"
            +" LEFT JOIN k_brand as b ON b.id = g.brand_id"
            +" LEFT JOIN k_goods_country as gc ON gc.gc_code1 = g.country"
            +" where g.id ='"+goodsid+"'";
        console.log(sql);
        DB.query(sql,function(er,list){
            if(!er){
                msg.success=true;
                msg.data = list;
                res.json(msg);
            }else{
                msg.msg=er;
                res.json(msg);
            }
        });
    } else {
        msg.msg="参数有误";
        res.json(msg);
    }
};

/**根据商品的id获取新品商品详情*/
exports.getNewGoodsDetailByid = function(req,res,msg){
    var newGoodId = req.body.newGoodId;
    console.log("newGoodId"+newGoodId);
    if (newGoodId){
        console.log("newGoodId"+newGoodId);
        var sql="select g.*,gs.person_number,gs.action_start_time,gs.action_end_time,gc.gc_name,b.brand_name from k_goods as g "
        +" LEFT JOIN k_goods_shop as gs ON g.id = gs.goods_id"
        +" LEFT JOIN k_brand as b ON b.id = g.brand_id"
        +" LEFT JOIN k_goods_country as gc ON gc.gc_code1 = g.country"
        +" where g.id ='"+newGoodId+"'";
        console.log(sql);
        DB.query(sql,function(er,list){
            console.log("----"+JSON.stringify(list));
            if(!er){
                msg.success=true;
                msg.data = list;
                res.json(msg);
            }else{
                msg.msg=er;
                res.json(msg);
            }
        });
    } else {
        msg.msg="参数有误";
        res.json(msg);
    }
};
//修改Is_new状态
exports.UpdateIs_new = function(req,res,msg){
    var newgood_id = req.body.newgood_id;
    var sql="Update k_goods_shop SET is_new = '0' WHERE goods_id ='"+newgood_id+"'";
    console.log(sql);
    DB.query(sql,function(er,list){
        console.log("----"+JSON.stringify(list));
        if(!er){
            msg.success=true;
            //msg.data = list;
            res.json(msg);
        }else{
            msg.msg=er;
            res.json(msg);
        }
    });
};

/**获取限时商品详情*/
exports.getzqProByshopId = function(req,res,msg){
    var shopid = req.body.shopid;
    var sql="select g.*,gs.price,gs.discount_price,gs.discount_start_time,gs.discount_end_time from k_goods_shop as gs LEFT JOIN k_goods as g ON g.id = gs.goods_id where gs.is_discount = '1' and gs.shop_id ='"+shopid+"'";
    console.log(sql);
    DB.query(sql,function(er,list){
        console.log("----"+JSON.stringify(list));
        if(!er){
            msg.success=true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg=er;
            res.json(msg);
        }
    });
};
/**修改Is_discount状态*/
exports.UpdateIs_discount = function(req,res,msg){
    var good_id = req.body.good_id;
    var sql="Update k_goods_shop SET is_discount = '0' WHERE goods_id ='"+good_id+"'";
    console.log(sql);
    DB.query(sql,function(er,list){
        console.log("----"+JSON.stringify(list));
        if(!er){
            msg.success=true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg=er;
            res.json(msg);
        }
    });
};

/**根据搜索关键字获取商品信息*/
exports.getSearchPro=function(req,res,msg){
    var searchPro = req.body.searchPro;
    var limit = req.body.limit;
    var sql = "SELECT *,shop_price as ProPrice FROM k_goods  WHERE goods_name LIKE '%" + searchPro +"%' LIMIT " + limit + ",6";
    console.log(sql);
    DB.query(sql,function(er,list){
        console.log("----"+JSON.stringify(list));
        if(!er){
            msg.success=true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg=er;
            res.json(msg);
        }
    });
};

/**获取积分商品*/
exports.getPointPro=function(req,res,msg){
    var goods_id = req.body.goods_id;
    if(goods_id){
        var sql = "SELECT g.*,gp.point,gp.point_price FROM k_goods_point as gp LEFT JOIN k_goods as g ON g.id = gp.goods_id WHERE is_sale = 1 AND goods_id = '"+goods_id+"'";
    }else{
        var sql = "SELECT g.*,gp.point,gp.point_price FROM k_goods_point as gp LEFT JOIN k_goods as g ON g.id = gp.goods_id WHERE is_sale = 1";
    }
    console.log(sql);
    DB.query(sql,function(er,list){
        console.log("----"+JSON.stringify(list));
        if(!er){
            msg.success=true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg=er;
            res.json(msg);
        }
    });
};

/**获取新人专享商品信息*/
exports.getNewProInfo=function(req,res,msg){
    var goods_id = req.body.goods_id;
    if(goods_id) {
        var sql = "SELECT g.*,ng.newperson_price FROM k_newperson_goods as ng LEFT JOIN k_goods as g ON g.id = ng.goods_id WHERE is_sale = 1 AND ng.goods_id = '"+goods_id+"'";
    }else{
        var sql = "SELECT g.*,ng.newperson_price FROM k_newperson_goods as ng LEFT JOIN k_goods as g ON g.id = ng.goods_id WHERE is_sale = 1 order by sort";
    }
    console.log(sql);
    DB.query(sql,function(er,list){
        console.log("----"+JSON.stringify(list));
        if(!er){
            msg.success=true;
            msg.data = list;
            res.json(msg);
        }else{
            msg.msg=er;
            res.json(msg);
        }
    });
};
